package org.kojo.myket.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.kojo.myket.dao.ItemDao;
import org.kojo.myket.entity.Item;
import org.kojo.myket.persistence.PMF;

import com.google.gson.Gson;
import com.google.gwt.core.client.impl.AsyncFragmentLoader.Logger;

public class ItemController extends HttpServlet {
	private final static String FIELD_NAME = "name";
	private final static String FIELD_PRICE ="price";
	private final static String FIELD_DESC = "desc";
	private final static String FIELD_STATUS="status";
	/**
	 * 
	 */
	private static final long serialVersionUID = 7668482333829351291L;
	
	private void processRequest(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {
		PrintWriter pw = resp.getWriter();
		System.out.println("ServletPath="+req.getServletPath());
		System.out.println("requestURL="+req.getRequestURL());
		System.out.println("pathInfo="+req.getPathInfo());
		System.out.println("pathTranslate="+req.getPathTranslated());
		System.out.println("contextPath="+req.getContextPath());
		String pathInfo = req.getPathInfo();
		if(pathInfo==null){
			resp.sendError(HttpServletResponse.SC_BAD_REQUEST,"Unknow command");
			return;
		}
		
		String[] command = pathInfo.split("/");
		System.out.println("command="+command);
		String process = command[1];
		if("add".equals(process)){
			String name = req.getParameter(FIELD_NAME);
			String price = req.getParameter(FIELD_PRICE);
			String status = req.getParameter(FIELD_STATUS);
			String desc = req.getParameter(FIELD_DESC);
			
			System.out.println("cmd to add ="+name);
			resp.setContentType("application/json");
			PersistenceManager pm = PMF.get().getPersistenceManager();
			Item item =  new Item();
			item.setName(name);
			item.setPrice(Double.parseDouble(price));
			item.setStatus(status);
			item.setDesc(desc);
			try{
				pm.makePersistent(item);
			}finally{
				pm.close();
			}
			Gson gson = new Gson();
			String output = gson.toJson(item);
			pw.print(output);
			pw.flush();
		}
		if("delete".equals(process)){
			System.out.println("cmd to delete");
			
		}
		if("list".equals(process)){
			System.out.println("list...");
			resp.setContentType("application/json");
			/*
			PersistenceManager pm = PMF.get().getPersistenceManager();
			Query q = pm.newQuery(Item.class);
			q.setRange(15, 20);
			
			//Object results = q.execute();
			//System.out.println("results="+results);
			
			List<Item> results = (List<Item>) q.execute();
			*/
			ItemDao itemDao = new ItemDao(Item.class);
			List<Item> results = itemDao.findRange(0, 10);
			Gson gson = new Gson();
			String output = gson.toJson(results);
			pw.print(output);
			pw.flush();
		}
		
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		processRequest(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		processRequest(req, resp);
	}

}
